package com.goddess.yeecolor.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.goddess.yeecolor.Modle.ActivityFragmentModle;
import com.goddess.yeecolor.R;
import com.goddess.yeecolor.tools.ImageUtils;

import java.util.List;

/**
 * 作者: CaoJunping
 * 时间: 2017/1/16
 * 作用: ******
 * 备注: 我的Fragment个人收藏Activity活动的Viewpager的Adapter
 */
public class ActivityFragmentAdapter extends BaseRecyclerAdapter<ActivityFragmentAdapter.MyCollectionViewHolder> {
    private Context context;
    private List<ActivityFragmentModle> modleList;

    public ActivityFragmentAdapter(Context context, List<ActivityFragmentModle> modleList) {
        this.context = context;
        this.modleList = modleList;
    }

    @Override
    public MyCollectionViewHolder getViewHolder(View view) {
        return new MyCollectionViewHolder(view);
    }

    @Override
    public MyCollectionViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View view=LayoutInflater.from(context).inflate(R.layout.activity_fragment_recycler_item,parent,false);
        return new MyCollectionViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyCollectionViewHolder holder, int position, boolean isItem) {
        ActivityFragmentModle modle=modleList.get(position);
        holder.tv_title.setText(modle.getTitle());
        holder.tv_date.setText(modle.getAddtime());
        holder.tv_time.setText(modle.getStart_time());
        holder.tv_address.setText(modle.getHost_location());
        holder.tv_thenumber.setText(modle.getPeople()+"");
        holder.tv_watchnumber.setText(modle.getViews()+"");
        ImageUtils.getInstance().ImageStart(context,modle.getThumb(),holder.img);
        holder.img.setScaleType(ImageView.ScaleType.FIT_XY);
    }

    @Override
    public int getAdapterItemCount() {
        return modleList.size();
    }

    class MyCollectionViewHolder extends RecyclerView.ViewHolder{
        private ImageView img;
        private TextView tv_title,tv_date,tv_time,tv_address,tv_thenumber,tv_watchnumber;
        public MyCollectionViewHolder(View itemView) {
            super(itemView);
            img= (ImageView) itemView.findViewById(R.id.img);
            tv_title= (TextView) itemView.findViewById(R.id.tv_title);
            tv_date= (TextView) itemView.findViewById(R.id.tv_date);
            tv_time= (TextView) itemView.findViewById(R.id.tv_time);
            tv_address= (TextView) itemView.findViewById(R.id.tv_address);
            tv_thenumber= (TextView) itemView.findViewById(R.id.tv_thenumber);
            tv_watchnumber= (TextView) itemView.findViewById(R.id.tv_watchnumber);

            /**
             * 每一行的点击事件
             */
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClicklistener.OnClickItem(v,getLayoutPosition()-1);
                }
            });
        }
    }

    /**
     * 每一行的点击事件接口
     */
    public interface OnItemClicklistener{
        void OnClickItem(View view, int position);
    }
    private OnItemClicklistener onItemClicklistener;

    public void setOnItemClicklistener(OnItemClicklistener onItemClicklistener){
        this.onItemClicklistener=onItemClicklistener;
    }

    /**
     *清空适配器
     */
    public void clear(){
        clear(modleList);
    }

    /**
     * 刷新事件
     * @param modle 刷新的实体类
     * @param position  添加到第几行
     */
    public void insert(ActivityFragmentModle modle,int position){
        insert(modleList,modle,position);
    }
}
